#!/usr/bin/python

if __name__ == '__main__':
	import sys
	import os
	import string
	import shutil

######################################
### color 
######################################
ATTRIBUTES = dict(
        zip([
            'bold',
            'dark',
            '',
            'underline',
            'blink',
            '',
            'reverse',
            'concealed'
            ],
            range(1, 9)
            )
        )
del ATTRIBUTES['']


HIGHLIGHTS = dict(
        zip([
            'on_grey',
            'on_red',
            'on_green',
            'on_yellow',
            'on_blue',
            'on_magenta',
            'on_cyan',
            'on_white'
            ],
            range(40, 48)
            )
        )


COLORS = dict(
        zip([
            'grey',
            'red',
            'green',
            'yellow',
            'blue',
            'magenta',
            'cyan',
            'white',
            ],
            range(30, 38)
            )
        )

RESET = '\033[0m'


def colored(text, color=None, on_color=None, attrs=None):
    """Colorize text.

    Available text colors:
        red, green, yellow, blue, magenta, cyan, white.

    Available text highlights:
        on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.

    Available attributes:
        bold, dark, underline, blink, reverse, concealed.

    Example:
        colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink'])
        colored('Hello, World!', 'green')
    """
    if os.getenv('ANSI_COLORS_DISABLED') is None:
        fmt_str = '\033[1;%dm%s'
        if color is not None:
            text = fmt_str % (COLORS[color], text)

        if on_color is not None:
            text = fmt_str % (HIGHLIGHTS[on_color], text)

        if attrs is not None:
            for attr in attrs:
                text = fmt_str % (ATTRIBUTES[attr], text)

        text += RESET
    return text
#####################################################

rm_dir = (".git", ".repo")
find_result = "/tmp/gitlist"
#print rm_dir[0]

def listgitdir(dir_path):
	if None == dir_path:
		return None
	command = 'find %s -name \"%s\" > %s' %(dir_path, rm_dir[0], find_result)
	#print command
	if 0!=os.system(command):
		print colored("find .git dir failed", "red")
	command = 'find %s -name \"%s\" >> %s' %(dir_path, rm_dir[1], find_result)
	if 0!=os.system(command):
		print colored("find .repo dir failed", "red")

def rmfile_in_gitlist():
	fi = open(find_result)
	for gitdir in fi.readlines():
		gitdir = string.replace(gitdir, "\n", "")
		if True==os.path.isdir(gitdir):
			#print "%s is dir" %gitdir
			if True==os.path.exists(gitdir):
				#print "read to remove dir : %s" %gitdir
				shutil.rmtree(gitdir)

def init_new_git(dir):
	if None==dir:
		return None
	command = 'cd %s && git init && git add . && git commit -a -m \'init code\'' %dir
	if False==os.system(command):
		print colored("init git failed", "green")

def main(orgin_argvs):
	dir = orgin_argvs[0][:]
	if True == os.path.isdir(dir):
		print colored("find the Dir, begin to remove the orgin .git dir", "red")
		listgitdir(dir)
		rmfile_in_gitlist()
		init_new_git(dir)
	else:
		print colored('Dir: %s is not exists', 'red')
	
	
if __name__ == '__main__':
	main(sys.argv[1:])
